<?php
//Initiate environment
define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
//current level in diretoty structure
$currentfolderlevel = 3;
array_splice($rootFolder,-$currentfolderlevel);
$base_folder = implode(DS,$rootFolder);
if(is_dir($base_folder.DS.'libraries'.DS.'joomla')) {
  define( '_JEXEC', 1 );
  define('JPATH_BASE',implode(DS,$rootFolder));
  require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); 
  require_once(JPATH_BASE .DS.'libraries/joomla/factory.php'); 
  $mainframe =& JFactory::getApplication('site');
  $mainframe->initialise();
  $db = &JFactory::getDBO();
  //End initiate environment
  $view = JRequest::getVar('view',''); 
  $cat = JRequest::getVar('cat','');
  switch($cat){
    case "default_content":
      $section = 'content';
      $querySec = "SELECT s.id, s.title 
                    FROM #__sections AS s WHERE s.scope = ".$db->Quote($section);     
      $db->setQuery($querySec);
      $section = $db->loadObjectList();
      if(count($section)>0){
        $aryData = array();
        $i=0;
        foreach($section as $item){
          $aryData[$i]['section'] = $item->title;
          $queryCat = "SELECT c.id, c.title 
                        FROM #__categories AS c 
                       WHERE c.published = 1 AND c.section = ".$db->Quote($item->id);         
          $db->setQuery($queryCat);
          $category = $db->loadObjectList(); 
          $j=0;         
          foreach($category as $itemCat){
            $aryDataCat[$j]['category_name'] = $itemCat->title;
            $aryDataCat[$j]['category_id'] = $itemCat->id;
            $j++;
          }
          $aryData[$i]['category'] = $aryDataCat;
          $i++;
        }       
      }    
      if($view == 'default'){
          $cid = JRequest::getVar('cid','');
          $filterContent = '';
          $aryCatId = array();
          getConfigModAccord($cid,$aryCatId,$filterContent,$tagHtml);
          $jsonData=array('jsonData'=>$aryData,'cIdConfig'=>$aryCatId,
                          'isDefaultCont'=>1,'filterContent'=>$filterContent,'tagHtml'=>$tagHtml);
      } else { 
          $jsonData = array('jsonData'=>$aryData,'isDefaultCont'=>1);
      }
      echo json_encode($jsonData);     
      break;
    case "k2_content":
      $db = &JFactory::getDBO();
      $query = 'SELECT m.* FROM #__k2_categories m WHERE published=1 AND trash = 0 ORDER BY parent, ordering';
        $db->setQuery( $query );
        $mitems = $db->loadObjectList();
        $children = array();
        if ($mitems){
            foreach ( $mitems as $v ){
                $pt     = $v->parent;
                $list = @$children[$pt] ? $children[$pt] : array();
                array_push( $list, $v );
                $children[$pt] = $list;
            }
        }       
        $list = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0 );
        $mitems = array();
        foreach ( $list as $item ) {
            $mitems[] = JHTML::_('select.option',  $item->id, '&nbsp;&nbsp;&nbsp;'.$item->treename );
        } 
        $i=0;
       foreach($mitems as $item){
         $aryData[$i]['catk2_id'] = $item->value;
         $aryData[$i]['catk2_title'] = $item->text;
         $i++;
       }
        if($view == 'default'){
            $cid = JRequest::getVar('cid','');           
            $aryCatId = array();
            $filterContent = '';
            getConfigModAccord($cid,$aryCatId,$filterContent,$tagHtml);
            $jsonData=array('jsonData'=>$aryData,'cIdConfig'=>$aryCatId,
                            'isDefaultCont'=>0,'filterContent'=>$filterContent,'tagHtml'=>$tagHtml);      
        } else {  
            $jsonData = array('jsonData'=>$aryData,'isDefaultCont'=>0); 
        }        
       echo json_encode($jsonData);
      break;  
  }     
}
function getConfigModAccord($cid,&$aryCatId,&$filterContent,&$tagHtml){
   $db = &JFactory::getDBO();
   $sql = "SELECT params FROM #__modules WHERE id=$cid";
   $db->setQuery($sql);
   $paramsConfigObj = $db->loadObjectList();
   $aryParams = explode("\n",$paramsConfigObj[0]->params); 
   foreach($aryParams as $item){
    $posSelection = strpos(trim($item),'selectionitem=');   
    if($posSelection === false){
    } else {
      $filterContent = substr(trim($item),$posSelection+(int)strlen('selectionitem='));
    }
    $posRemoveHtml = strpos(trim($item),'tag_html=');   
    if($posRemoveHtml === false){
    } else {
      $tagHtml = substr(trim($item),$posRemoveHtml+(int)strlen('tag_html='));
    }       
    $pos = strpos($item,'catid=');     
    if($pos === false){       
    } else {     
       $aryCatId = explode('|', substr($item,$pos+(int)strlen('catid=')));
    }    
   }
}
